<?php
/**
* $Id: muser.php 2942 2009-11-30 10:48:18Z huong.pham $
*/
class MUser extends TV_Model 
{
    protected $table = "users";
    
    function MUser()
    {
        parent::TV_Model(); 
        //Sample connect another db: parent::QS_Model("oldhouseweb"); 
        $this->selectDefault();
    }
    
    /* Default fields to select 
    *  @access : public     
    */
    public function selectDefault(){
        $this->selected_columns = "username, email, first_name, last_name";
        return $this;
    }
    
    public function selectDetails(){
        $this->selected_columns = "*";
        return $this;
    }
    
    /**
    * Set rules of validation
    * 
    */
    public function rules()
    {
        return array(
            'login' => array(
                'email' => 'trim|required|xss_clean',
                'password' => 'trim|required|callback_loginCheck'
            ),
            'lost_password' =>array(
                'userEmail' => 'trim|required|xss_clean|callback_emailCheck'
            )
        );
    }
    
    //Validation field name
    public function fields(){
        return array(
            'username' => 'Username',
            'password' => 'Password',
            'userEmail' => 'Email'
        );
    }
    
    /**
    * Update user info
    * 
    * @param mixed $condition
    */
    public function update($data, $condition)
    {
        $this->db->update($this->table, $data, $condition);    
    }    
        
    /**
    * check user by email and password
    * 
    * @param mixed $email
    * @param mixed $password
    */
    
    public function getByEmailAndPassword($email, $password = NULL)
    {
        $data = array();
        $sql = "SELECT {$this->selected_columns}
               FROM {$this->table}
               WHERE email = ?";
        $data[] = $email;
        if(!empty($password))
        {
            $sql .= 'AND password = ?';
            $data[] = $password;            
        }
        return $this->query_row($sql, $data);
    }
        
    /**
    * All state by status
    * 
    * @param mixed $status
    */
    public function findAll()
    {
        $sql = " SELECT {$this->selected_columns}
                FROM {$this->table}
        ";
        return $this->query_array($sql);    
    }
}